package com.shanghaionstar.user.service;

import android.content.Context;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.api.v3.UserService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.joinme.RegisterRequestEntity;
import com.onstar.cn.more.response.HeadImgRes;
import com.onstar.cn.more.response.PersonalInfoResponse;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.LinkedHashMap;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by 7828wc on 16/7/1.
 */
public class AccountInfoService extends BaseService<PersonalInfoResponse> {
    private Context context;

    public AccountInfoService(Context context) {
        this.context = context;
    }

    @Override
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public RequestEntity getRequestEntity() {
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        UserService publicService = new UserService(Constants.CURRENT_SERVER_URL);
        requestEntity = publicService.getPresonalInfomation(requestEntity);
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "getRequestEntity", requestEntity);
        return requestEntity;
    }


    @Override
    public PersonalInfoResponse jsonToBean(String json) {
        return GsonHelper.fromJson( json,PersonalInfoResponse.class);
    }

    public Observable<String> init() {
        return Observable.defer(new Func0<Observable<String>>() {
            @Override
            public Observable<String> call() {
                return Observable.just("init");
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public Observable<PersonalInfoResponse> updateUserInfo(final String licenseExpireDate) {
        return Observable.defer(new Func0<Observable<PersonalInfoResponse>>() {
            @Override
            public Observable<PersonalInfoResponse> call() {
                try {
                    UsersService publicService = new UsersService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
                    coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
                    requestEntity.setCustomHeads(coHashMap);
                    RegisterRequestEntity presonal = new RegisterRequestEntity();
                    presonal.setLicenseExpireDate(licenseExpireDate);
                    presonal.setUserName(ApplicationUtil.getInstance().getIdpID());
                    requestEntity = publicService.getPhoneAndnumber(requestEntity, presonal);
                    requestEntity = HttpInjectUtils.injectHttp(AccountInfoService.this.getClass(), "updateUserInfo", requestEntity, String.class);

                    return Observable.just((PersonalInfoResponse) GsonHelper.fromJson(BusinessUtil.getResponse(getDatas(requestEntity), context),PersonalInfoResponse.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public Observable<HeadImgRes> getObserval() {
        return Observable.defer(new Func0<Observable<HeadImgRes>>() {
            @Override
            public Observable<HeadImgRes> call() {
                try {
                    UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    String idpId = ApplicationUtil.getInstance().getIdpID();
                    requestEntity = service.getHeadPhoto(requestEntity, idpId);
                    requestEntity = HttpInjectUtils.injectHttp(AccountInfoService.this.getClass(), "getObserval", requestEntity);

                    HeadImgRes headImgRes = GsonHelper.fromJson(BusinessUtil.getResponse(getDatas(requestEntity), context),HeadImgRes.class);
                    return Observable.just(headImgRes);
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });

    }
}
